package icst.cyber.hrm.actions;

import icst.cyber.hrm.bo.JobBO;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author NGOCLAN
 * 
 */
public class JobAction extends HttpServlet {
	
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        JobBO jobBO = new JobBO();
        
        try {
	        String act = request.getParameter("act");        
	        if(act.equals("new")){
	        	String name = request.getParameter("txtName");
	        	jobBO.insertJob(name);
	        	
	        }else if(act.equals("edit")){
	        	int id = Integer.parseInt(request.getParameter("hideId"));
	        	String name = request.getParameter("txtName");
	        	jobBO.updateJob(id, name);
	        	
	        }else if(act.equals("delete")){
	        	int id = Integer.parseInt(request.getParameter("hideId"));
	        	jobBO.deleteJob(id);
	        	
	        }
	        response.sendRedirect("Manager.do?opt=config&task=job");
	        
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
        
        
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
